package main

import (
	"fmt"
	"time"

	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
)

type MyMainWindowUI struct {
	userNameLineEdit  *walk.LineEdit
	passWordLineEdit  *walk.LineEdit
	rememberPwdChk    *walk.CheckBox
	automaticLoginChk *walk.CheckBox
	loginBtn          *walk.PushButton
	cancelBtn         *walk.PushButton
	clearConfigBtn    *walk.PushButton
	messageLabel      *walk.Label
}

type MyMainWindow struct {
	*walk.MainWindow
	mw MainWindow
	ui MyMainWindowUI
}

//初始化窗口
func (this *MyMainWindow) init() (err error) {
	this.MainWindow = new(walk.MainWindow)
	this.mw.AssignTo = &this.MainWindow
	this.mw.Layout = VBox{}
	//this.mw.FixedSize = true
	//this.mw.Maximize = true
	this.initWidget()

	if err = this.mw.Create(); err != nil {
		return err
	}
	if ic, err := walk.NewIconFromFile("icon.ico"); err != nil {
		return err
	} else {
		this.MainWindow.SetIcon(ic)
	}
	this.MainWindow.SetTitle("内网登录")
	this.MainWindow.SetBounds(walk.Rectangle{400, 200, 550, 170})
	this.MainWindow.SetMinMaxSize(walk.Size{300, 170}, walk.Size{300, 170})
	return nil
}

func (this *MyMainWindow) run() int {
	return this.MainWindow.Run()
}

//初始化控件
func (this *MyMainWindow) initWidget() (err error) {
	this.mw.Children = []Widget{
		Label{
			AssignTo: &this.ui.messageLabel,
			Text:     "内网登录",
			Font: Font{
				PointSize: 10,
				Family:    "微软雅黑",
			},
		},
		LineEdit{
			AssignTo:  &this.ui.userNameLineEdit,
			MinSize:   Size{0, 25},
			CueBanner: "用户名 / 邮箱",
			Font: Font{
				PointSize: 10,
				Family:    "微软雅黑",
			},
		},
		LineEdit{
			AssignTo:     &this.ui.passWordLineEdit,
			MinSize:      Size{0, 25},
			PasswordMode: true,
			CueBanner:    "密码",
			Font: Font{
				PointSize: 10,
				Family:    "微软雅黑",
			},
		},
		HSplitter{
			Children: []Widget{
				CheckBox{
					AssignTo: &this.ui.automaticLoginChk,
					Text:     "自动登陆",
				},
				PushButton{
					AssignTo: &this.ui.loginBtn,
					Text:     "登陆",
					MinSize:  Size{0, 30},
				},
				PushButton{
					AssignTo: &this.ui.cancelBtn,
					Text:     "关闭",
					MinSize:  Size{0, 30},
				},
				PushButton{
					Text:     "删除配置",
					AssignTo: &this.ui.clearConfigBtn,
					MinSize:  Size{0, 30},
				},
			},
		},
	}
	return nil
}

//初始化事件
func (this *MyMainWindow) initEvent(cfg *Config) {
	ui := this.ui
	//登录按钮
	ui.loginBtn.Clicked().Attach(func() {
		if ui.userNameLineEdit.Text() == "" {
			walk.MsgBox(this.MainWindow, "错误", "请输入您的用户名！", walk.MsgBoxIconInformation)
			return
		} else if ui.passWordLineEdit.Text() == "" {
			walk.MsgBox(this.MainWindow, "错误", "请输入您的密码！", walk.MsgBoxIconInformation)
			return
		}
		ui.messageLabel.SetText("正在登录...")
		cfg.LoginName = ui.userNameLineEdit.Text()
		passwd, err := encryptAndBase64Encode([]byte(ui.passWordLineEdit.Text()))
		if err != nil {
			ui.messageLabel.SetText("系统出错!")
			return
		}
		cfg.Passwd = passwd
		res, err := login(cfg)
		if err != nil {
			ui.messageLabel.SetText("系统出错!")
		} else {
			ui.messageLabel.SetText(res.Msg)
		}
	})

	//取消按钮
	ui.cancelBtn.Clicked().Attach(func() {
		this.MainWindow.Close()
	})

	//消除配置按钮
	ui.clearConfigBtn.Clicked().Attach(func() {
		if err := delConfig(); err != nil {
			ui.messageLabel.SetText("删除配置失败!")
		} else {
			ui.messageLabel.SetText("删除配置成功!")
		}
	})

	//自动登陆
	ui.automaticLoginChk.CheckedChanged().Attach(func() {
		cfg.AutomaticLogin = ui.automaticLoginChk.Checked()
		err := writeConfig(cfg)
		if err != nil {
			ui.messageLabel.SetText("系统出错!")
		}
	})
	ui.automaticLoginChk.SetChecked(cfg.AutomaticLogin)
}

func main() {
	//1 创建主窗体
	//1.1 初始化窗体
	//2.2 初始化控件
	//3 初始化事件
	//4 运行
	var err error
	mw := new(MyMainWindow)
	err = mw.init()
	if err != nil {
		fmt.Println("mw.init err :", err)
		return
	}
	go loginIntranet(mw)
	r := mw.run()
	fmt.Println("exit :", r)
}

//登录
func loginIntranet(this *MyMainWindow) {
	time.Sleep(time.Second / 2)
	cfg, err := readConfig()
	if err != nil {
		fmt.Println("readConfig err :", err)
		return
	}
	ui := this.ui

	ui.userNameLineEdit.SetText(cfg.LoginName)
	if string(cfg.Passwd) != "" {
		rdPwd, err := decryptAndBase64Decode(cfg.Passwd)
		if err != nil {
			fmt.Println("decryptAndBase64Decode err :", err)
			return
		}
		ui.passWordLineEdit.SetText(string(rdPwd))
	}
	if cfg.AutomaticLogin {
		res, err := login(cfg)
		if err != nil {
			ui.messageLabel.SetText("系统出错!")
		} else {
			ui.messageLabel.SetText(res.Msg)
		}
	}
	//初始化事件
	this.initEvent(cfg)
}
